package com.xoa.service.attendance.rule.impl;

import com.xoa.dao.attendance.AttendRuleMapper;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.model.attendance.AttendRule;
import com.xoa.service.attendance.rule.AttendRuleService;
import com.xoa.util.ToJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AttendRuleServiceImpl implements AttendRuleService {

    @Autowired
    private AttendRuleMapper attendRuleMapper;

    @Autowired
    DepartmentMapper departmentMapper;
    /**
     * 获取主页数据
     * @return
     */
    @Override
    public ToJson getAttendRule() {
        ToJson toJson=new ToJson();
        List<AttendRule> attendRuleList=attendRuleMapper.getAttendRuleList();
        for (int i=0;i<attendRuleList.size();i++){
            String DeptName="";
            AttendRule attendRule=attendRuleList.get(i);
            if(attendRule.getDeptId()!=null && !"".equals(attendRule.getDeptId())){
            String [] DeptId=attendRule.getDeptId().split(",");
                if("ALL_DEPT".equals(DeptId[0])){
                    DeptName="全体部门";
                }else{
                    for (int j=0 ;j<DeptId.length;j++) {
                        DeptName+=departmentMapper.getDeptNameByDeptId(Integer.parseInt(DeptId[j]))+",";
                    }
                }
            }
            attendRule.setDeptName(DeptName);
        }
        if(attendRuleList!=null && attendRuleList.size()>0){
            toJson.setObj(attendRuleList);
            toJson.setFlag(0);
            toJson.setMsg("true");
        }else{
            toJson.setMsg("false");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 新增统计规则
     * @param attendRule
     * @return
     */
    @Override
    public ToJson insertAttendRule(AttendRule attendRule) {
        attendRule.setArid(null);
        ToJson toJson=new ToJson();
        int result=0;
        String deptName="";
        if(attendRule!=null) {
            String dept=this.isRepetitionDept(attendRule);
            //查看此部门是否已经有统计规则了
            if(dept==null){
                result= attendRuleMapper.insertSelective(attendRule);
            }else{
                if("ALL_DEPT".equals(dept)){
                    deptName="所有部门";
                }else{
                    deptName=departmentMapper.getDeptNameByDeptId(Integer.parseInt(dept));
                }
            }
        }
        if(result>0){
            toJson.setFlag(0);
            toJson.setMsg("成功");
        }else{
            toJson.setObject(deptName);
            toJson.setMsg("失败");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 获取编辑数据
     * @param id
     * @return
     */
    @Override
    public ToJson getAttendRuleById(Integer id) {
        ToJson toJson=new ToJson();
        AttendRule attendRule=attendRuleMapper.selectByPrimaryKey(id);
        String DeptName="";
        if(attendRule.getDeptId()!=null && !"".equals(attendRule.getDeptId())) {
            String[] DeptId = attendRule.getDeptId().split(",");
            if ("ALL_DEPT".equals(DeptId[0])) {
                DeptName = "全体部门";
            } else {
                for (int j = 0; j < DeptId.length; j++) {
                    DeptName += departmentMapper.getDeptNameByDeptId(Integer.parseInt(DeptId[j])) + ",";
                }
            }
        }
        attendRule.setDeptName(DeptName);
        if(attendRule!=null){
            toJson.setObject(attendRule);
            toJson.setFlag(0);
            toJson.setMsg("成功");
        }else{
            toJson.setMsg("失败");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 编辑
     * @param attendRule
     * @return
     */
    @Override
    public ToJson updateAttendRule(AttendRule attendRule) {
        ToJson toJson=new ToJson();
        int result=0;
        String deptName="";
        if(attendRule!=null && attendRule.getArid()!=null && !"".equals(attendRule.getArid())){
            String dept=this.isRepetitionDept(attendRule);
            if(dept==null){
                result=attendRuleMapper.updateByPrimaryKeySelective(attendRule);
            }else{
                if("ALL_DEPT".equals(dept)){
                    deptName="所有部门";
                }else{
                    deptName=departmentMapper.getDeptNameByDeptId(Integer.parseInt(dept));
                }
            }
        } if(result>0){
            toJson.setFlag(0);
            toJson.setMsg("成功");
        }else{
            toJson.setObject(deptName);
            toJson.setMsg("失败");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ToJson deleteAttendRule(Integer id) {
        ToJson toJson=new ToJson();
        int result= attendRuleMapper.deleteByPrimaryKey(id);
        if(result>0){
            toJson.setFlag(0);
            toJson.setMsg("成功");
        }else{
            toJson.setMsg("失败");
            toJson.setFlag(1);
        }
        return toJson;


    }

    /**
     * 查看当前部门是否已经有统计规则了
     * @param attendRule
     * @return
     */
    public String isRepetitionDept(AttendRule attendRule){
        if(attendRule!=null){
            String [] deptIdArrays=attendRule.getDeptId().split(",");
            //判断是修改还是添加
            if(deptIdArrays!=null &&deptIdArrays.length>0){
                List<AttendRule> attendRulesList=attendRuleMapper.getAttendRuleList();
                if(attendRule.getArid()!=null&& !"".equals(attendRule.getArid())){
                for(int i=0;i<attendRulesList.size();i++){
                    AttendRule attendR = attendRulesList.get(i);
                    if (attendRule.getArid().equals(attendR.getArid())){
                        attendRulesList.remove(i);
                    }
                }
                }
                if("ALL_DEPT".equals(deptIdArrays[0])){
                    if(attendRulesList.size()<=0){
                        return null;
                    }else if(attendRulesList.size()==1 && "ALL_DEPT".equals(attendRulesList.get(0).getDeptId())){
                        return "ALL_DEPT";
                    }else if(attendRulesList.get(0).getDeptId()!=null && !"".equals(attendRulesList.get(0).getDeptId())){
                        return attendRulesList.get(0).getDeptId().substring(0,attendRulesList.get(0).getDeptId().indexOf(","));
                    }
                }else if(attendRulesList.size()==1 && "ALL_DEPT".equals(attendRulesList.get(0).getDeptId())){
                    return "ALL_DEPT";
                }else{
                    for(int i=0;i<attendRulesList.size();i++){
                        AttendRule attendR = attendRulesList.get(i);
                        String [] strs=attendR.getDeptId().split(",");
                        for (String str:strs) {
                            for (String deptIdArray:deptIdArrays) {
                                if(deptIdArray.equals(str)){
                                    return str;
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

}
